﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MvcMall.AdminPortal.Controllers.SysSettings.ViewModels;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;
using MvcMall.Framework.File;
using MvcMall.Framework.Mapper;
using SharpArch.Web.NHibernate;
using MvcMall.Framework.Utility;

namespace MvcMall.AdminPortal.Controllers.SysSettings
{
    public class SysSettingsController : BaseController
    {
        private readonly ISysSettingsTask _sysSettingTask;
        private readonly IMapper<SysSetting, SysSettingModel> _sysSettingViewModelMapper;
        private readonly IMapper<SysSettingModel, SysSettingCommand> _sysSettingCommandMapper;
        private readonly IFileUploadHelper _iFileUploadHelper;

        public SysSettingsController(ISysSettingsTask sysSettingTask, IMapper<SysSetting, SysSettingModel> sysSettingViewModelMapper, IMapper<SysSettingModel, SysSettingCommand> sysSettingCommandMapper, IFileUploadHelper fileUploadHelper)
        {
            this._sysSettingTask = sysSettingTask;
            this._sysSettingViewModelMapper = sysSettingViewModelMapper;
            this._sysSettingCommandMapper = sysSettingCommandMapper;
            this._iFileUploadHelper = fileUploadHelper;
        }


        public ActionResult Index()
        {
            var model = _sysSettingViewModelMapper.MapFrom(_sysSettingTask.GetDefaultSetting());
            return View(model);
        }

        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(SysSettingModel model)
        {
            if (!ModelState.IsValid)
            {

                return View(model);
            }

            string defaultShoppingImg = model.DefGoodsImg;
            string defaultLogo = model.DefShopImg;
            string defaultUserHead = model.DefUserImg;

            HttpFileCollectionBase files = HttpContext.Request.Files;
            if (files != null && files.Count > 0)
            {
                // ReSharper disable PossibleNullReferenceException
                if (files["defaultShopImg"] != null && files["defaultShopImg"].InputStream.Length > 0)
                {
                    model.DefGoodsImg = _iFileUploadHelper.UpdateLoadImgSaveSoure(files["defaultShopImg"], files["defaultShopImg"].FileName);
                }
                if (files["defaultLogo"] != null && files["defaultLogo"].InputStream.Length > 0)
                {
                    model.DefShopImg = _iFileUploadHelper.UpdateLoadImgSaveSoure(files["defaultLogo"], files["defaultLogo"].FileName);
                }
                if (files["defaultUserHead"] != null && files["defaultUserHead"].InputStream.Length > 0)
                {
                    model.DefUserImg = _iFileUploadHelper.UpdateLoadImgSaveSoure(files["defaultUserHead"], files["defaultUserHead"].FileName);
                }
                // ReSharper restore PossibleNullReferenceException
            }


            var dataModel = _sysSettingCommandMapper.MapFrom(model);
            var domainModel = _sysSettingTask.Save(dataModel);

            #region///用于处理删除旧的文件信息
            if (!string.IsNullOrEmpty(defaultShoppingImg) && defaultShoppingImg != domainModel.DefGoodsImg)
            {
                string deleteFilePath = Utils.GetWebSiteBasePath() ;
                if (Utils.FileExists(deleteFilePath + defaultShoppingImg))
                {
                    Utils.DeleteFile(deleteFilePath + defaultShoppingImg);
                } 
            }
            if (!string.IsNullOrEmpty(defaultLogo) && defaultLogo != domainModel.DefShopImg)
            {
                string deleteFilePath = Utils.GetWebSiteBasePath() ;
                if (Utils.FileExists(deleteFilePath + defaultLogo))
                {
                    Utils.DeleteFile(deleteFilePath + defaultLogo);
                } 
            }
            if (!string.IsNullOrEmpty(defaultUserHead) && defaultUserHead != domainModel.DefUserImg)
            {
                string deleteFilePath = Utils.GetWebSiteBasePath();
                if (Utils.FileExists(deleteFilePath + defaultUserHead))
                {
                    Utils.DeleteFile(deleteFilePath + defaultUserHead);
                } 
            }
            #endregion
            return ShowMsg(domainModel != null ? "保存成功" : "保存失败");


        }


    }
}
